package com.ziqikeji.xianyan.controller.recommend;

import com.ziqikeji.xianyan.common.DatagridDTO;
import com.ziqikeji.xianyan.entity.recommend.ArticleRecommend;
import com.ziqikeji.xianyan.entity.recommend.AuthorRecommend;
import com.ziqikeji.xianyan.enumeration.DeleteFlagEnum;
import com.ziqikeji.xianyan.service.recommend.AuthorRecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @author cyy
 * Created by home on 2019/10/14.
 */
@Controller
@RequestMapping(value = "admin/AuthorRecommend")
public class AuthorRecommendController{

    @Autowired
    AuthorRecommendService authorRecommendService;

    @RequestMapping(value = "AuthorRecommend")
    public String authorRecommendList(){
        return "/admin/recommend/author/AuthorRecommend";
    }

    /**
     * 已推荐作者列表
     */
    @ResponseBody
    @RequestMapping(value = "AuthorRecommendList/YES",method = RequestMethod.GET)
    public DatagridDTO getArticleRecommendList(@RequestParam(value = "pageNumber",required = false,defaultValue = "1") String pageNumber,
                                               @RequestParam(value = "pageSize",required = false,defaultValue = "10") String pageSize
    ){
        Page pageImpl = null;
        try{
            int page = 0;
            int size = 10;
            if (!StringUtils.isEmpty(pageNumber)) {
                page = Integer.parseInt(pageNumber) - 1;
                page = page < 0 ? 0 : page;
            }
            if (!StringUtils.isEmpty(pageSize)) {
                size = Integer.parseInt(pageSize);
                size = size < 0 ? 0 : size;
            }
            Page<AuthorRecommend> articlePage = this.authorRecommendService.getPage(page,size);
            PageRequest pageRequest = PageRequest.of(page,size);
            pageImpl = new PageImpl<>(articlePage.getContent(), pageRequest, articlePage.getTotalElements());

        }catch (Exception e){
            e.printStackTrace();
            return new DatagridDTO("获取失败，错误信息：" + e.getMessage(), 1, new ArrayList());
        }
        return new DatagridDTO("列表获取成功", 0, pageImpl.getTotalElements(), pageImpl.getContent());
    }

    /**
     * 保存推荐
     */
    @PostMapping(value = "saveAuthorRecommend")
    public String saveAuthorRecommend(AuthorRecommend recommend){
        this.authorRecommendService.save(recommend);
        //重定向
        return "redirect:/admin/AuthorRecommend/recommendList";
    }

    /**
     * 取消推荐
     */
    @RequestMapping(value = "cancelAuthorRecommend/{id}")
    public String cancelAuthorRecommend(@PathVariable("id")String id){
        AuthorRecommend authorRecommend = this.authorRecommendService.get(id);
        //设置删除标记
        authorRecommend.setDeleteFlagEnum(DeleteFlagEnum.DELETE);
        this.authorRecommendService.update(authorRecommend);
        return "redirect:/admin/AuthorRecommend/recommendList";
    }

    /**
     * 查看作者信息
     */
}